<template>
  <div>
    <template>
      <el-input v-model="input" placeholder="姓名或账户"></el-input>
      <el-button type="primary" @click="change(input)">搜索</el-button>
      <el-button type="primary" @click="add">添加</el-button>
      <el-table
        :data="table.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        border
        style="width: 100%,height: 200px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          v-for="(item, index) in tableTitle"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          width="180"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template scope="{row}">
            <el-link
              type="warning"
              style="margin: 0 5px"
              @click="handleEdit(row, 'edit')"
              >编辑</el-link
            >
            <!-- @click="handleEdit(row, 'edit')" -->
            <el-link
              type="warning"
              style="margin: 0 5px"
              @click="handleEdit(row, 'del')"
              >删除</el-link
            >
            <!-- @click="handleEdit(row, 'del')" -->
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="序号" prop="id">
            <el-input disabled v-model="ruleForm.id"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="账户" prop="tle">
            <el-input v-model="ruleForm.tle"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-input v-model="ruleForm.sex"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="最后时间" prop="date">
            <el-input v-model="ruleForm.date"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="quxiao('ruleForm')">取 消</el-button>
          <el-button type="primary" @click="queding('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-dialog>
       <el-pagination
      style="margin: 12px 0px"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="table.length"
    >
    </el-pagination>
    </template>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import "../mock/index";
export default {
  data() {
    return {
        // 分页
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      total: 0,
      searchContent: "",
      dialogTitle: "编辑",
      input: "",
      dialogVisible: false,
      ruleForm: {
        id: "",
        title: "",
        name: "",
        tle: "",
        sex: "",
        date: "",
      },
      rules: {
        id: [{ required: true, message: "请输入序号", trigger: "blur" }],
        name: [{ required: true, message: "请输入名字", trigger: "blur" }],
        tle: [{ required: true, message: "请输入账户", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        title: [{ required: true, message: "请输入备注", trigger: "blur" }],
        date: [{ required: true, message: "请输入时间", trigger: "blur" }],
      },
      tableData: [],
      tableTitle: [
        {
          prop: "id",
          label: "序号",
        },
        {
          prop: "name",
          label: "姓名",
        },
        {
          prop: "tle",
          label: "账户",
        },
        {
          prop: "sex",
          label: "性别",
        },
        {
          prop: "title",
          label: "备注",
        },
        {
          prop: "date",
          label: "最后时间",
        },
      ],
    };
  },
  created() {
    this.$store.dispatch("getList");
    // console.log(this.table);
  },
  computed: {
    ...mapState(["table"]),
  },
  methods: {
      // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
    change(input){
      // this.$store.dispatch("getList");
      setTimeout(() => {
        this.search(input)
      }, 500);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    ...mapMutations(["addNum", "del", "edit",'search']),
    add() {
      this.ruleForm.id = new Date() * 1;
      this.dialogTitle = "新增";
      this.dialogVisible = true;
    },
    quxiao() {
      // this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.ruleForm = {
        id: "",
        title: "",
        name: "",
        sex: "",
        date: "",
        tle: "",
      };
    },

    queding(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // alert('submit!');
          if (this.dialogTitle == "新增") {
            this.addNum(this.ruleForm);
            this.dialogVisible = false;
          } else {
            this.edit(this.ruleForm);
            this.quxiao("ruleForm");
            this.dialogVisible = false;
            this.$message({
              type: "success",
              message: "编辑完成",
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    handleEdit(row, type) {
      // console.log(index, row);//
      if (type == "del") {
        this.$confirm("此操作删除该条记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.del(row);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.ruleForm.title = row.title;
        this.ruleForm.date = row.date;
        this.ruleForm.name = row.name;
        this.ruleForm.sex = row.sex;
        this.ruleForm.id = row.id;
        this.ruleForm.tle = row.tle;
        this.dialogVisible = true;
      }
    },
  },
};
</script>

<style>
.el-table td.el-table__cell,
.el-table th.el-table__cell.is-leaf {
  text-align: center;
}
.el-input {
  width: 200px;
  margin: 0 20px;
}
</style>